import type { repUserInfo, repWxUserDtosInfo } from '@/services/login'
import type { cardData } from '@/types/card'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

// 定义 Store
export const useOrderStore = defineStore(
  'order',
  () => {
    // 订单信息
    const profile = ref<cardData[]>()
    const clearOrderList = () => {
      profile.value = []
    }
    const addOrderList = (list:cardData[]) => {
      clearOrderList()
      profile.value = list
    }
    const allPrice = computed(()=>{
      return profile.value?.reduce((sum,item)=>sum+item.count*item.price,0)
    })

    // 记得 return
    return {
      profile,
      clearOrderList,
      addOrderList,
      allPrice
    }
  },
  // TODO: 持久化
  {
    // persist: true,
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
